<?php
//自定义页面
namespace app\common\controller;
use app\common\model\AdminDepartment;
use app\common\model\AdminMenu;
use app\common\model\AdminPower;
use app\common\model\AdminRole;
use app\common\model\AdminUser;
use app\common\middleware\Auth;
use think\facade\Request;
class Power
{
    protected $middleware = [Auth::class => []];
    public function Index()
    {
        return ViewTpl('common/power/index');
    }

    public function Mark()
    {
        if (Request::isAjax() || Request::isPost()) {
            $type = input("type") ?: 'role';
            $rows = [];
            switch ($type) {
                case "role":
                    $rows = AdminRole::where("is_delete=0")->field("id,name,num")->select()->toArray();
                    break;
                case "user":
                    $rows = AdminUser::where("is_delete=0 and status=1")->field("id,name,user as num")->select()->toArray();
                    break;
                case "department":
                    $rows = AdminDepartment::where("is_delete=0")->field("id,name,num,parent_id")->select()->toArray();
                    break;
            }
            $data['rows'] = $rows;
            $data['total'] = count($rows);
            return BackCode($data);
        }
    }


    public function Cat()
    {
        if (Request::isAjax() || Request::isPost()) {
            $type = input("type") ?: 'menu';
            $mark = input("mark") ?: 'mark';
            $mark_id = input("mark_id") ?: 'mark_id';
            $find = AdminPower::where("cat='" . $type . "' and mark='" . $mark . "' and mark_id='" . $mark_id . "' and is_delete=0")->field('cat_ids')->findOrEmpty()->toArray();
            $cat_ids = !empty($find['cat_ids']) ? $find['cat_ids'] : '';
            $cat_ids = explode(',', $cat_ids);
            $ids = '';
            foreach ($cat_ids as $k => $v) {
                $ids.= '[' . $v . ']';
            }

            $rows = AdminMenu::where("status=1 and is_delete=0")->field("id,title as name,parent_id")->order("sort asc,create_time desc")->select()->toArray();
            foreach ($rows as $k => $v) {
                $rows[$k]['num'] = '-';
                $LAY_CHECKED = false;
                if (strpos($ids, '[' . $v['id'] . ']') !== false) {
                    $LAY_CHECKED = true;
                }
                $rows[$k]['LAY_CHECKED'] = $LAY_CHECKED;
            }
            $data['rows'] = $rows;
            $data['total'] = count($rows);
            return BackCode($data);
        }
    }

    public function Edit()
    {
        if (Request::isAjax() || Request::isPost()) {
            $AdminPower = new AdminPower();
            $mark = input("mark");
            if (empty($mark)) {
                return BackCode([], 203, 'mark参数不能为空');
            }
            $mark_id = input("mark_id");
            if (empty($mark_id)) {
                return BackCode([], 203, 'mark_id参数不能为空');
            }
            $cat = input("cat");
            if (empty($cat)) {
                return BackCode([], 203, 'cat参数不能为空');
            }
            $find = $AdminPower->where("cat='" . $cat . "' and mark='" . $mark . "' and mark_id='" . $mark_id . "'")->field('id')->findOrEmpty()->toArray();
            $upData = [
                'mark' => $mark,
                'mark_id' => $mark_id,
                'cat' => $cat,
                'cat_ids' => input("cat_ids"),
                'update_time' => date('Y-m-d H:i:s'),
                'update_id' => GetToken('id'),
                'tenant_id' => GetToken('tenant_id') ? : 0,
            ];
            if (empty($find)) {
                $upData['id'] =GetSnowID();
                $upData['create_time'] = $upData['update_time'];
                $upData['create_id'] = $upData['update_id'];
                $AdminPower->insert($upData);
            } else {
                $AdminPower->where("id='" . $find['id'] . "'")->update($upData);
            }
            return BackCode();
        }
    }
}